package com.zp.business.controller.lab;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.lab.bo.LabItemBo;
import com.zp.business.entity.lab.vo.LabItemVo;
import com.zp.business.service.lab.ILabItemService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 检验申请子表控制器
 * 前端访问路由地址为:/business/labItem
 *
 * @author zhang peng
 * @ date 2024-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/labItem")
public class LabItemController extends BaseController {

    private final ILabItemService iLabItemService;

    /**
     * 查询检验申请子表列表 (分页)
     */
    @SaCheckPermission("business:labItem:list")
    @GetMapping("/selectPage")
    public TableDataInfo<LabItemVo> selectPage(LabItemBo bo, PageQuery pageQuery) {
        return iLabItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询检验申请子表列表 (不分页)
     */
    @SaCheckPermission("business:labItem:list")
    @GetMapping("/list")
    public R<List<LabItemVo>> list(LabItemBo bo) {
        List<LabItemVo> list = iLabItemService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出检验申请子表列表
     */
    @SaCheckPermission("business:labItem:export")
    @Log(title = "检验申请子表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LabItemBo bo, HttpServletResponse response) {
        List<LabItemVo> list = iLabItemService.queryList(bo);
        ExcelUtil.exportExcel(list, "检验申请子表", LabItemVo.class, response);
    }

    /**
     * 获取检验申请子表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:labItem:list")
    @GetMapping("/{id}")
    public R<LabItemVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iLabItemService.queryById(id));
    }

    /**
     * 新增检验申请子表
     */
    @SaCheckPermission("business:labItem:add")
    @Log(title = "检验申请子表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LabItemBo bo) {
        return toAjax(iLabItemService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改检验申请子表
     */
    @SaCheckPermission("business:labItem:edit")
    @Log(title = "检验申请子表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LabItemBo bo) {
        return toAjax(iLabItemService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除检验申请子表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:labItem:remove")
    @Log(title = "检验申请子表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iLabItemService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
